<template>
    <var-action-sheet
        :actions="actions"
        v-model:show="visible"
        @select="onSelect"
    />
</template>
<script setup>
import { GetWorkPay, GetOrderStatus } from "~/api";

const emits = defineEmits(["success"]);
const visible = ref(false);
const data = ref({});
const userStore = useUserStore();
const pageStore = usePageStore();
const actions = [
    {
        name: "支付宝",
        icon: `${_oss_}images/mobile/icon-alipay.png`,
        color: "#1296db",
        type: "alipay",
    },
    {
        name: "微信",
        icon: `${_oss_}images/mobile/icon-weixin.png`,
        color: "#0ABC64",
        type: "weixin",
    },
];
let work_id = ref(null);
let timer = null;
let pay_channel = null;
let back_order_no = null;

const refreshOrder = () => {
    timer = setTimeout(async () => {
        const res = await GetOrderStatus({
            order_no: back_order_no,
            type: 3,
        });

        if (res.data == 1) {
            Message().success("支付成功！");
            clearTimeout(timer);
            visible.value = false;
            emits("success");
        } else {
            refreshOrder();
        }
    }, 3000);
};

const onPay = async ({ pay_channel }) => {
    let params = {
        pay_channel,
        order_no: back_order_no,
        work_id: work_id.value,
        code: pageStore.code,
        openid: userStore.openid,
        redirect_url: pageStore.isMobile && !IsWeiXin() ? location.href : "",
    };
    let res = await GetWorkPay(params);
    if (res.code == 200) {
        const { order_no, content } = res.data;
        back_order_no = order_no;

        if (content.openid) userStore.openid = content.openid;
        if (pay_channel === 1) {
            if (!IsWeiXin()) {
                window.location.href = content;
            } else {
                WeixinJSBridge.invoke(
                    "getBrandWCPayRequest",
                    content,
                    function (res) {
                        if (res.err_msg == "get_brand_wcpay_request:ok") {
                        } else if (
                            res.err_msg == "get_brand_wcpay_request:cancel"
                        ) {
                            Message().info("您取消了支付");
                        } else if (
                            res.err_msg == "get_brand_wcpay_request:fail"
                        ) {
                            Message().error("您取消了支付");
                        } else {
                            Message().warning(
                                typeof res.err_msg != "undefined"
                                    ? res.err_msg
                                    : typeof res.errMsg !== "undefined"
                                    ? res.errMsg
                                    : "未知支付状态"
                            );
                        }
                    }
                );
            }
        } else {
            alipayForm(content);
        }

        refreshOrder();
    } else {
        Message().error(res.msg);
    }
};

const onSelect = (action) => {
    if (action.type === "weixin") {
        pay_channel = 1;
        onPay({ pay_channel });
    } else if (action.type === "alipay") {
        pay_channel = 2;
        onPay({ pay_channel });
    }
};

defineExpose({
    show({ id }) {
        back_order_no = null;
        visible.value = true;
        work_id.value = id;
    },
});
</script>
